// pages/detailList/detailList.js
const request =require("../../utils/request")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categories: {},
    shops: [],//九宫格进来对应的列表数据
    pageIndex: 0,//页码
    pageSize: 20,//每页的数据 
    totalCount: 0,
    hasMore: true//标记
  },
  LoadList(){
    let { pageIndex, pageSize, searchText  } = this.data  //解构赋值
    const params = { _page: ++pageIndex, _limit: pageSize }
    if (searchText) params.q = searchText

    console.log(params)
    return request(`/categories/${this.data.categories.id}/shops`, params)
      .then(res=>{
        console.log(res)
        const totalCount = parseInt(res.header['x-total-count']) 
        const hasMore = this.data.pageIndex * this.data.pageSize < totalCount
        const shops = this.data.shops.concat(res.data)

        this.setData({
          shops,
          totalCount,
          pageIndex,
          hasMore
        })

      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.cat)
    request(`/categories/${options.cat}`).then((res)=>{
      this.setData({
        categories:res.data,
        name:res.data.name
      })
      wx.setNavigationBarTitle({
      title:res.data.name,
    })
    this.LoadList()
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.LoadList()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({ shops: [], pageIndex: 0, hasMore: true })
    this.LoadList().then(() => wx.stopPullDownRefresh())
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.LoadList()
  },
  searchHandle() {
    console.log(this.data.searchText)
    this.setData({ shops: [], pageIndex: 0, hasMore: true })
    this.LoadList()
  },

  showSearchHandle() {
    this.setData({ searchShowed: true })
  },
  hideSearchHandle() {
    this.setData({ searchText: '', searchShowed: false })
  },
  clearSearchHandle() {
    this.setData({ searchText: '' })
  },
  searchChangeHandle(e) {
    console.log(e)
    this.setData({ searchText: e.detail.value })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})